package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.pojo.ClassInstructorTeacher;
import com.zhentao.pojo.Instructor;
import com.zhentao.pojo.Teacher;
import com.zhentao.pojo.User;
import com.zhentao.pojo.Student;
import com.zhentao.service.ClassInstructorTeacherService;
import com.zhentao.service.InstructorService;
import com.zhentao.service.TeacherService;
import com.zhentao.service.UserService;
import com.zhentao.service.StudentService;
import com.zhentao.vo.Query;
import com.zhentao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RequestMapping("teacher")
@RestController
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private InstructorService instructorService;
    @Autowired
    private ClassInstructorTeacherService classInstructorTeacherService;
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;

    @RequestMapping("findTeacher")
    public Result findTeacher(@RequestBody Query query){
        Page<Teacher> teacher = teacherService.pageTeacherWithClassesAndInstructors(query);
        return Result.OK(teacher);
    }

    @RequestMapping("addTeacher")
    public Result addTeacher(@RequestBody Teacher teacher){
        teacher.setHireDate(new Date());
        teacherService.save(teacher);

        // 生成用户账号
        User user = new User();
        user.setName(teacher.getPhone());
        user.setPassword("123456"); // 明文，service里会自动加密
        user.setRid(3); // 角色id
        userService.insertUser(user); // 一定加密

        return Result.OK();
    }

    @RequestMapping("delTeacher")
    public Result delTeacher(Integer id){
        teacherService.removeById(id);
        return Result.OK();
    }

    @RequestMapping("findTeacherById")
    public Result findTeacherById(Integer id){
        Teacher byId = teacherService.getById(id);
        return Result.OK(byId);
    }

    @RequestMapping("updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher){
        teacherService.updateById(teacher);
        return Result.OK();
    }

    @RequestMapping("findInstructor")
    public Result findInstructor(@RequestBody Query query){
        Page<Instructor> instructorPage = instructorService.pageInstructorWithClassesAndTeachers(query);
        return Result.OK(instructorPage);
    }

    @RequestMapping("addInstructor")
    public Result addInstructor(@RequestBody Instructor instructor){
        instructor.setHireDate(new Date());
        instructorService.save(instructor);

        // 生成用户账号
        User user = new User();
        user.setName(instructor.getPhone());
        user.setPassword("123456"); // 明文，service里会自动加密
        user.setRid(4); // 角色id
        userService.insertUser(user); // 一定加密
        return Result.OK();
    }

    @RequestMapping("delInstructor")
    public Result delInstructor(Integer id){
        instructorService.removeById(id);
        return Result.OK();
    }

    @RequestMapping("findInstructorById")
    public Result findInstructorById(Integer id){
        Instructor byId = instructorService.getById(id);
        return Result.OK(byId);
    }

    @RequestMapping("updateInstructor")
    public Result updateInstructor(@RequestBody Instructor instructor){
        instructorService.updateById(instructor);
        return Result.OK();
    }

    @RequestMapping("addCIT")
    public Result addCIT(@RequestBody ClassInstructorTeacher classInstructorTeacher){
        classInstructorTeacherService.save(classInstructorTeacher);
        return Result.OK("绑定成功");
    }

    /**
     * 修改班级的讲师导员绑定关系
     */
    @RequestMapping("updateCIT")
    public Result updateCIT(Integer classId, Integer newTeacherId, Integer newInstructorId) {
        if (classId == null || newTeacherId == null || newInstructorId == null) {
            return Result.ERROR("班级ID、新讲师ID、新导员ID不能为空");
        }
        
        boolean success = classInstructorTeacherService.updateClassBinding(classId, newTeacherId, newInstructorId);
        if (success) {
            return Result.OK("绑定关系修改成功");
        } else {
            return Result.ERROR("绑定关系修改失败");
        }
    }

    /**
     * 查询班级当前的绑定关系
     */
    @RequestMapping("getCITByClass")
    public Result getCITByClass(Integer classId) {
        if (classId == null) {
            return Result.ERROR("班级ID不能为空");
        }
        
        ClassInstructorTeacher binding = classInstructorTeacherService.getBindingByClassId(classId);
        return Result.OK(binding);
    }

    /**
     * 分页查询导员（包含班级和讲师信息）
     */
    @RequestMapping("pageInstructorDetail")
    public Result pageInstructorDetail(@RequestBody Query query) {
        if (query.getCurrent() == null) {
            query.setCurrent(1);
        }
        if (query.getSize() == null) {
            query.setSize(10);
        }

        Page<Instructor> page = instructorService.pageInstructorWithClassesAndTeachers(query);
        return Result.OK(page);
    }

    /**
     * 根据ID查询讲师详情（包含班级和导员信息）
     */
    @RequestMapping("findTeacherDetail")
    public Result findTeacherDetail(Integer teacherId) {
        if (teacherId == null) {
            return Result.ERROR("讲师ID不能为空");
        }

        Teacher teacher = teacherService.getTeacherWithClassesAndInstructors(teacherId);
        if (teacher == null) {
            return Result.ERROR("讲师不存在");
        }

        return Result.OK(teacher);
    }

    /**
     * 根据ID查询导员详情（包含班级和讲师信息）
     */
    @RequestMapping("findInstructorDetail")
    public Result findInstructorDetail(Integer instructorId) {
        if (instructorId == null) {
            return Result.ERROR("导员ID不能为空");
        }

        Instructor instructor = instructorService.getInstructorWithClassesAndTeachers(instructorId);
        if (instructor == null) {
            return Result.ERROR("导员不存在");
        }

        return Result.OK(instructor);
    }

    /**
     * 查询教师所带所有学生
     */
    @RequestMapping("listStudentsByTeacher")
    public Result listStudentsByTeacher(Integer teacherId) {
        if (teacherId == null) {
            return Result.ERROR("教师ID不能为空");
        }
        List<Student> students = studentService.getStudentsByTeacherId(teacherId);
        return Result.OK(students);
    }
}
